package com.hengpeng.itfin.tools;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 线程池服务类
 * @author weiyuan
 */
public class PoolServer {
	
	public static Log logger = LogFactory.getLog(PoolServer.class);
	
	private ThreadPoolExecutor executor;//创建线程执行器
	
	public PoolServer (int poolSize) {
		/**
		 * 创建一个固定数量的线程池
		 */
		this.executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(poolSize);
	}
	/**
	 * 添加任务
	 * @param task
	 */
	public void executeTask (Runnable runnable) {
		logger.info("服务 ： 一个新的任务已抵达");
		this.executor.execute(runnable);
		logger.info("当前线程池大小：" + this.executor.getTaskCount());
		logger.info("当前线程池活跃线程数量：" + this.executor.getActiveCount());
		logger.info("当前线程池已完成线程数量：" + this.executor.getCompletedTaskCount());
	}
	/**
	 * 结束
	 */
	public void endServer () {
		this.executor.shutdown();
	}
}
